package org.miniframework.util;

import java.lang.reflect.Type;

public class ConverterUtil {

    /**
     * 返回基本数据类型的空值
     * 需要特殊处理的基本类型即int\double\short\long\byte\float\boolean
     * @param paramType 参数类型
     * @return 对应的空值
     */
    public static Object primitiveNull(Class<?> paramType) {
        if (paramType == int.class || paramType == double.class ||
            paramType == short.class || paramType == long.class ||
            paramType == byte.class || paramType == float.class){
            return 0;
        }else if (paramType == boolean.class){
            return false;
        }
        return null;
    }

    /**
     * String类型转换成对应的参数类型
     * @param paramType         参数类型
     * @param requestParamValue 值
     * @return 转换后的Object
     */
    public static Object convert(Class<?> paramType, String requestParamValue) {
        // 判断是否非基础类型参数转换，目前仅仅支持基础类型转换
        if (isPrimitive(paramType)){
            if (ValidationUtil.isEmpty(requestParamValue)){
                return primitiveNull(paramType);
            }
            if (paramType.equals(int.class) || paramType.equals(Integer.class)){
                return Integer.parseInt(requestParamValue);
            }else if (paramType.equals(String.class)){
                return requestParamValue;
            } else if (paramType.equals(Double.class) || paramType.equals(double.class)) {
                return Double.parseDouble(requestParamValue);
            } else if (paramType.equals(Float.class) || paramType.equals(float.class)) {
                return Float.parseFloat(requestParamValue);
            } else if (paramType.equals(Long.class) || paramType.equals(long.class)) {
                return Long.parseLong(requestParamValue);
            } else if (paramType.equals(Boolean.class) || paramType.equals(boolean.class)) {
                return Boolean.parseBoolean(requestParamValue);
            } else if (paramType.equals(Short.class) || paramType.equals(short.class)) {
                return Short.parseShort(requestParamValue);
            } else if (paramType.equals(Byte.class) || paramType.equals(byte.class)) {
                return Byte.parseByte(requestParamValue);
            }
            return requestParamValue;
        }else{
            throw new RuntimeException("count not support non primitive type conversion yet");
        }
    }

    /**
     * 判定是否基本数据类型(包括包装类以及String)
     * @param type 参数类型
     * @return 是否为基本数据类型
     */
    private static boolean isPrimitive(Class<?> type) {
        return type == boolean.class
                || type == Boolean.class
                || type == double.class
                || type == Double.class
                || type == float.class
                || type == Float.class
                || type == short.class
                || type == Short.class
                || type == int.class
                || type == Integer.class
                || type == long.class
                || type == Long.class
                || type == String.class
                || type == byte.class
                || type == Byte.class
                || type == char.class
                || type == Character.class;
    }
}
